package com.sgb.zhaocai.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.sgb.link.entity.PickInvitedApply;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.FragmentPickInvitedListBinding;
import com.sgb.zhaocai.view.activity.PickInvitedSignUpActivity;
import com.sgb.zhaocai.view.adapter.PickInvitedApplyAdapter;
import com.sgb.zhaocai.viewmodel.PickInvitedApplyListModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;

/**
 * 邀请报名/邀请竞价
 */
public class PickInvitedApplyFragment extends AppLazyFragment {

    private FragmentPickInvitedListBinding binding;
    private PickInvitedApplyListModel model;
    private String fromWhere;   //1.招标 2.竞价
    private PickInvitedApplyAdapter pickInvitedApplyAdapter;
    private boolean isMore = false;
    private String searchName;

    public static PickInvitedApplyFragment newInstance(String fromWhere) {
        PickInvitedApplyFragment fragment = new PickInvitedApplyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("fromWhere", fromWhere);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_invited_list, container, false);
        binding.setLifecycleOwner(this);
        model = new ViewModelProvider(this).get(PickInvitedApplyListModel.class);
        fromWhere = getArguments().getString("fromWhere");
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        initAdapter();
        registerObserver();
    }

    private void initData() {
        if ("1".equals(fromWhere)) {
            model.findSignPage(searchName);
        } else if ("2".equals(fromWhere)) {
            model.getFindBidPage(searchName);
        }
    }

    private void initAdapter() {

        pickInvitedApplyAdapter = new PickInvitedApplyAdapter(getContext(), null);
        pickInvitedApplyAdapter.setFromWhere(fromWhere);
        pickInvitedApplyAdapter.onIChildClick(new PickInvitedApplyAdapter.IChildClick() {
            @Override
            public void onClick(PickInvitedApply pickInvitedApply) {
                PickInvitedSignUpActivity.start(getContext(),pickInvitedApply,fromWhere);
            }
        });

        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_list_divide_jc));
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerView.setAdapter(pickInvitedApplyAdapter);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isMore = false;
                model.page.set(1);
                initData();
            }

            @Override
            public void onLoadMore() {
                isMore = true;
                model.page.set(model.page.get() + 1);
                initData();
            }
        });

        binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                switch (newState){
                    /*正在拖拽*/
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        /*惯性滑动中*/
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        int position = manager.findLastVisibleItemPosition();
                        if (position > 10) {
                            binding.imgBackTop.setVisibility(View.VISIBLE);
                        } else {
                            binding.imgBackTop.setVisibility(View.GONE);
                        }
                        break;
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

            }
        });

        binding.imgBackTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.imgBackTop.setVisibility(View.GONE);
                binding.recyclerView.getLayoutManager().scrollToPosition(0);
            }
        });
    }

    private void registerObserver() {
        model.dialog.observe(this, aBoolean -> {
            if (aBoolean) {
                if (!DialogHelper.isShowing()) {
                    DialogHelper.showProgressDialog(getContext(), null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                }
            } else {
                DialogHelper.dismissProgressDialog();
            }
        });
        model.pickInvitedApplyList.observe(this, new Observer<List<PickInvitedApply>>() {
            @Override
            public void onChanged(List<PickInvitedApply> lists) {
                binding.recyclerView.refreshComplete();
                if(binding.imgBackTop.getVisibility() == View.GONE){
                    if(isMore && lists.size()>0){
                        binding.imgBackTop.setVisibility(View.VISIBLE);
                    }else{
                        binding.imgBackTop.setVisibility(View.GONE);
                    }
                }
                if (isMore) {
                    binding.recyclerView.loadMoreComplete();
                    if (lists.size() > 0) {
                        pickInvitedApplyAdapter.addDatas(lists);
                    } else {
                        binding.recyclerView.setLoadingMoreEnabled(false);
                    }
                } else {
                    binding.recyclerView.refreshComplete();
                    if (lists.size() > 0) {
                        binding.empty.setVisibility(View.GONE);
                        binding.recyclerView.setVisibility(View.VISIBLE);
                        pickInvitedApplyAdapter.setDatas(lists);
                        binding.recyclerView.setLoadingMoreEnabled(true);
                    } else {
                        pickInvitedApplyAdapter.setDatas(new ArrayList<>());
                        binding.recyclerView.setVisibility(View.GONE);
                        binding.empty.setVisibility(View.VISIBLE);
                    }
                }
                pickInvitedApplyAdapter.notifyDataSetChanged();
            }
        });
        model.showToast.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MToast.showToast(getActivity(), s);
            }
        });
    }

    public void updateData(String searchName, String fromWhere) {
        this.searchName = searchName;
        this.fromWhere = fromWhere;
        if (model != null) {
            initData();
        }
    }
}